package com.ysh.sqldemo;

import com.ysh.bean.Student;
import com.ysh.mapper.StudentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;

/*
    动态sql测试类
 */
public class IfSqlTest {

    private SqlSession sqlSession;
    private StudentMapper mapper;
    private InputStream is;

    @Before
    public void before() throws Exception{
        is = Resources.getResourceAsStream("MyBatisConfig.xml");

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

        sqlSession = sqlSessionFactory.openSession();

        mapper = sqlSession.getMapper(StudentMapper.class);

    }
    @After
    public void After() throws Exception{
        sqlSession.commit();
        sqlSession.close();
        is.close();
    }
    @Test
    public void findByCondition() throws Exception{
        Student stu = new Student();
        stu.setId(1);
        // stu.setName("迪丽热巴");
        // stu.setAge(28);

        List<Student> students = mapper.selectCondition(stu);
        for (Student student : students) {
            System.out.println(student);
        }
    }

}
